// Version 15.1
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 */
@isTest
private class TestCaseFromYPTrigger 
{
 
    static testMethod void TestTrigger() 
    {
    	ID abuseRecordTypeID =  RecordTypeHelper.GetRecordTypeID('Case', 'IOG Incident');
 
        string phone0 = '(415) 999-2222';
        string phone1 = '(222) 888-3333';
        string phone1bis = '222-888 3333';
        string phone2 = '(333) 777-4444';
        string phone3 = 'bad phone';
        
        string companyName0 = 'UNIT_TEST 1492';
        string companyName1 = 'UNIT_TEST 2483';
        string companyName2 = 'UNIT_TEST 3574';

        string afCustomerId0 = '11119999';
        string afCustomerId1 = '22228888';
        
        Account a1 = new Account();
        a1.AFrontier_CustomerID__c = afCustomerId1;
        a1.Name = companyName1;
        a1.RecordTypeId = SilCustomerHelper.GetLocalAdvertiserAccountRecordType();
        insert a1;

        Contact c1 = new Contact();
        c1.LastName = companyName1;
        c1.Phone = phone2;
        c1.AccountID = a1.ID;
        c1.RecordTypeId = SilCustomerHelper.GetLocalAdvertiserContactRecordType();
        insert c1;
        
        Account a2 = new Account();
        a2.Name = companyName2;
        a2.RecordTypeId = SilCustomerHelper.GetLocalAdvertiserAccountRecordType();
        insert a2;

        Contact c2 = new Contact();
        c2.LastName = companyName2;
        c2.Phone = phone2;
        c2.AccountID = a2.ID;
        c2.RecordTypeId = SilCustomerHelper.GetLocalAdvertiserContactRecordType();
        insert c2;
        
        Account a3 = new Account();
        a3.Name = companyName2;
        a3.RecordTypeId = SilCustomerHelper.GetLocalAdvertiserAccountRecordType();
        insert a3;

        Contact c3 = new Contact();
        c3.LastName = companyName2;
        c3.Phone = phone2;
        c3.AccountID = a3.ID;
        c3.RecordTypeId = SilCustomerHelper.GetLocalAdvertiserContactRecordType();
        insert c3;
        
        Account a4 = new Account();
        a4.Name = companyName2;
        a4.RecordTypeId = SilCustomerHelper.GetLocalAdvertiserAccountRecordType();
        insert a4;
        
        Contact c4 = new Contact();
        c4.LastName = companyName2;
        c4.Phone = phone1bis;
        c4.AccountID = a4.ID;
        c4.RecordTypeId = SilCustomerHelper.GetLocalAdvertiserContactRecordType();
        insert c4;

        // Defunct: Test 0: Find zero phone, find zero names (phone0, name0)
        // Defunct: Test 1: Find zero phone, find one name  (phone0, name1)
        // Defunct: Test 2: Find zero phone, find multiple names (phone0, name2)
        
        // Defunct: Test 3: Find one phone (phone1, name0)
        
        // Defunct: Test 4: Find many phone, find zero names (phone2, name0)             
        // Defunct: Test 5: Find many phone, find one names (phone2, name1)              
        // Defunct: Test 6: Find many phone, find multiple names (phone2, name2)

        // Test 7: Find by AF Customer ID (afCustomerId1)
        // Test 8: Don't find by AF Customer ID (afCustomerId0)
        // Defunct: Test 9: Bad Phone, find zero names (phone3, name0)

        List<Case> caseList = new List<Case>();
        integer numOfTests = 2;
        integer numOfObjects = 200;
        integer mod;
        Case c;
                
        for(integer i = 0; i < numOfObjects; i++)
        {
            c = new Case();
            c.Origin = 'Web';
            c.Web_Source__c = 'UNIT TEST';
            c.Subject = 'Unit Test no:' + i;
            
            mod = Math.Mod(i, numOfTests);
            if(mod == 0)
            {
                c.RecordTypeID = abuseRecordTypeID;
                c.Web_CMR_AFCustomerID__c = afCustomerId1;              
            }
            else if(mod == 1)
            {
                c.RecordTypeID = abuseRecordTypeID;
                c.Web_CMR_AFCustomerID__c = afCustomerId0;
            }

            caseList.add(c);
        }
        
        Test.startTest();
        
        insert caseList;
        
        Test.stopTest();
        
        
        List<Case> resultcaseList = [Select Id, AccountID, Subject from Case where ID IN :caseList];
        Map<ID, Case> mappedCases = new Map<ID, Case>();
        for(Case resultCase : resultcaseList)
        {
            mappedCases.put(resultCase.ID, resultCase);
        }
        
        ID expectedAccountID;
        for(integer i = 0; i < numOfObjects; i++)
        {   
            mod = Math.Mod(i, numOfTests);
            if(mod == 0) // Test 7
            {
                expectedAccountID = a1.ID;              
            }
            else if(mod == 1) // Test 8
            {
                expectedAccountID = null;               
            }
            
            Case resultCase =mappedCases.get(caseList[i].ID);  
            System.debug('Assertion for Test: ' + mod + ' Case: ' + resultCase );
            System.assertEquals (expectedAccountID, resultCase.AccountID);
        }
        
    }
}